package com.dzy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dzy.base.BaseDao;
import com.dzy.base.BaseServiceImpl;
import com.dzy.dao.DictDao;
import com.dzy.dao.HouseDao;
import com.dzy.entity.House;
import com.dzy.service.HouseService;
import com.dzy.vo.HouseQueryVo;
import com.dzy.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service(interfaceClass = HouseService.class, timeout = 600000)
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseDao houseDao;
    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }


    @Override
    public House getById(Serializable id) {
        House house = super.getById(id);
        //houseTypeId : houseTypeName
        String houseTypeName = this.dictDao.getNameById(house.getHouseTypeId());
        house.setHouseTypeName(houseTypeName);
        //floorId : floorName
        String floorName = this.dictDao.getNameById(house.getFloorId());
        house.setFloorName(floorName);
        //buildStructureName
        String buildStructureName = this.dictDao.getNameById(house.getBuildStructureId());
        house.setBuildStructureName(buildStructureName);
        //directionName
        String directionName = this.dictDao.getNameById(house.getDirectionId());
        house.setDirectionName(directionName);
        //decorationName
        String decorationName = this.dictDao.getNameById(house.getDecorationId());
        house.setDecorationName(decorationName);
        //houseUseName
        String houseUseName = this.dictDao.getNameById(house.getHouseUseId());
        house.setHouseUseName(houseUseName);
        return house;
    }

    @Override
    public PageInfo<HouseVo> findListPage(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {
        //启用分页插件
        PageHelper.startPage(pageNum,pageSize);
        //调用业务层获取分页数据(SQL语句不需要手动的写limit，交给PageHelper插件)
        Page<HouseVo> page=this.houseDao.findListPage(houseQueryVo);
        List<HouseVo> houseVoList = page.getResult();
        for (HouseVo houseVo : houseVoList) {
            String houseTypeName = this.dictDao.getNameById(houseVo.getHouseTypeId());
            houseVo.setHouseTypeName(houseTypeName);
            String floorName = this.dictDao.getNameById(houseVo.getFloorId());
            houseVo.setFloorName(floorName);
            String directionName = this.dictDao.getNameById(houseVo.getDirectionId());
            houseVo.setDirectionName(directionName);
        }
        //返回结果
        return new PageInfo<>(page.getResult());
    }
}
